package com.example.controller;

import com.example.common.JwtInterceptor;
import com.example.common.Result;
import com.example.entity.AirQualitySensor;
import com.example.entity.Params;
import com.example.service.AirQualitySensorService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@ResponseBody
@RestController
@CrossOrigin
@RequestMapping("/AQS")
public class AirQualitySensorController {

    private static final Logger log = LoggerFactory.getLogger(JwtInterceptor.class);
    @Resource
    private AirQualitySensorService airQualitySensorService;


    @GetMapping("/getSensorData")
    public Result getSensorData(Params params) {
        PageInfo<AirQualitySensor> search = airQualitySensorService.getSensorData(params);
        return Result.success(search);
    }

    @PostMapping("/addAQS")
    public Result addAQS(@RequestBody AirQualitySensor airQualitySensor) {
        airQualitySensorService.addAQS(airQualitySensor);
        return Result.success();
    }

    @DeleteMapping("/deleteAQS/{id}")
    public Result deleteAQS(@PathVariable Long id) {
        airQualitySensorService.deleteAQS(id);
        return Result.success();
    }

    @GetMapping("/echarts/line")
    public Result line(Params params) {
        log.info("params" + params);
        // 防止 interval 传了非法数值，比如负数0小时
        if (params.getInterval() == null || params.getInterval() <= 0 || params.getInterval() > 24) {
            params.setInterval(3) ; // 给一个合理默认值
        }
        Map<String, Object> data = airQualitySensorService.initLine(params);
        return Result.success(data);
    }

    @GetMapping("/echarts/initLineForAdmin")
    public Result initLineForAdmin(Params params) {
        if (params.getPast() == null || params.getPast() <= 0) {
            params.setPast(3); ; // 给一个合理默认值
        }
        Map<String, Object> data = airQualitySensorService.initLineForAdmin(params);
        return Result.success(data);
    }


}
